package com.sinoglobal.dumping.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;

import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.dumping.dumplingspring.DumplingObservable;
import com.sinoglobal.dumping.util.Dumpling_AntiViolenceLisntener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by KeZhuang on 2015/10/29.
 */
public class Dumpling_SinoBaseFragment extends Fragment implements Dumpling_AntiViolenceLisntener{
    private Toast toast;
    public Dumpling_SinoBaseActivity parentActivity;
    private List<Dumpling_SinoAsyncTask> sThreadList = new ArrayList<Dumpling_SinoAsyncTask>();
    private long lastClickTime;

    @SuppressWarnings("unchecked")
    public <T> T getView(int resId){
        if(parentActivity!=null){
            return (T) parentActivity.findViewById(resId);
        }
        return (T)getActivity().findViewById(resId);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (Dumpling_SinoBaseActivity) getActivity();
    }

    /**
     * 添加一个网络请求任务
     * @param task
     */
    public Dumpling_SinoAsyncTask addTask(Dumpling_SinoAsyncTask task){
        sThreadList.add(task);
        return task;
    }

    public DumplingObservable getObservable(){
        if(parentActivity!=null){
            return parentActivity.getObservable();
        }
        return ((Dumpling_MainActivity)getActivity()).getObservable();
    }

    public Dumpling_MainActivity getParentActivity() {
        return (Dumpling_MainActivity) parentActivity;
    }
    /**
     * 清理掉所有的网络请求任务
     */
    public void clearTask(){
        if(sThreadList.size()>0){
            for(Dumpling_SinoAsyncTask task : sThreadList){
                task.cancel(true);
            }
            sThreadList.clear();
        }
    }

    /**
     * 执行所有网络请求任务
     */
    public void executeTask(){
        for(Dumpling_SinoAsyncTask task : sThreadList){
            task.execute();
        }
    }

    /**
     * 显示一个toast
     *
     * @param message toast的内容
     */
    public void showToast(String message) {
        if (message == null) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
        } else {
            toast.setText(message);
        }
        toast.show();
    }
    /**
     * 显示一个Toast
     *
     * @param message 吐司的内容
     * @param time    吐司停留的时间
     */
    public void showToast(String message, int time) {
        if (message == null && time != -1) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
        } else {
            toast.setText(message);
        }
        toast.show();
    }
    /**
     * 隐藏View方法,此方法隐藏会保留View的大小
     *
     * @param view 需要隐藏的View
     */
    public void hideView(View... view) {
        if (view.length > 0) {
            for (View viewParams : view) {
                viewParams.setVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * 隐藏View方法,此方法隐藏不保留View的大小
     *
     * @param view 需要隐藏的View
     */
    public void hideViewForGone(View... view) {
        if (view.length > 0) {
            for (View viewParams : view) {
                viewParams.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 显示View方法
     *
     * @param view 需要显示的View
     */
    public void showView(View... view) {
        if (view.length > 0) {
            for (View viewParams : view) {
                viewParams.setVisibility(View.VISIBLE);
            }
        }
    }
    @Override
    public void onStop() {
        super.onStop();
        clearTask();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        clearTask();
    }

    @Override
    public void onAntiViolence(View view) {

    }

    @Override
    public boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if((time - lastClickTime) > 0 && (time - lastClickTime) < 1000){
            return true;
        }else{
            lastClickTime = time;
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        if(!isFastDoubleClick()){
            onAntiViolence(v);
        }
    }

    public Context getContext() {
        return getActivity();
    }
}
